home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_179 / unixutil / detab.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  132 lines

  1. /* detab.c - expand tabs to spaces in a file.  If a single tabstop    *
  2.  *    is given, tabs are tabstop spaces apart, otherwise tabstops    *
  3.  *    are at <tab1>, <tab2>,..., <tabn>.  If no file is specified,    *
  4.  *    standard input is read and standard output written.        *
  5.  *                                    *
  6.  * detab [-<tab1> -<tab2>... -<tabn>] [filename... ]            *
  7.  *                                    *
  8.  * detab (C) 1988 by Gary L. Brant                    *
  9.  *                                    *
  10.  * :ts=8                                */
  11.  
  12. #define MAXLINE 1000
  13. #include <stdio.h>
  14. char tabarray[MAXLINE];
  15.  
  16. main (argc, argv)    /* remove trailing blanks and tabs and */
  17.             /* expand tabs in source lines */
  18. int argc;
  19. char *argv[];
  20. {
  21.    int i = 0, j, k, l, ntabs = 0, tabstop = 8;
  22.    int argtabs[99];
  23.    char ch, *pch;
  24.    FILE *ifile, *fopen ();
  25.  
  26.    /* decode tabstop arguments */
  27.    while ((++i < argc) && (argv[i][0] == '-')) {
  28.       j = 1;
  29.       tabstop = 0;
  30.       while ((ch = argv[i][j++]) != '\0') {
  31.      if (ch >= '0' && ch <= '9') {
  32.         tabstop *= 10;
  33.         tabstop += ch - '0';
  34.      } else {
  35.         fputs ("bad args\n", stderr);
  36.         exit (20);
  37.      }
  38.       }
  39.       argtabs[ntabs++] = tabstop;
  40.    }
  41.  
  42.    /* fill tabarray with \1 for each tabstop position */
  43.    for (k = 0; k < MAXLINE; k++)
  44.       tabarray[k] = '\0';
  45.    if (ntabs > 1)
  46.       for (k = 0; k < ntabs; k++)
  47.      if ((l = argtabs[k]-1) < MAXLINE)
  48.         tabarray[l] = '\001';
  49.      else {
  50.         fputs ("bad tab specification\n", stderr);
  51.         exit (20);
  52.      }
  53.    else if ((tabstop > 0) && (tabstop < MAXLINE))
  54.       for (k = tabstop; k < MAXLINE; k += tabstop)
  55.      tabarray[k] = '\001';
  56.    else {
  57.       fputs ("bad tab specification\n", stderr);
  58.       exit (20);
  59.    }
  60.  
  61.    /* remaining arguments should be file names - detab them */
  62.    if (i < argc) {
  63.       while (i < argc)
  64.      if ((ifile = fopen ((pch = argv[i++]), "r")) == NULL) {
  65.         fputs ("detab: cant open ", stderr);
  66.         fputs (pch, stderr);
  67.         putc ('\n', stderr);
  68.         exit (20);
  69.      } else
  70.         detab (ifile);
  71.    } else
  72.       detab (stdin);
  73. }
  74.  
  75.  
  76. /* detab - remove the tabs from one file */
  77.  
  78. detab (ifile)
  79. FILE *ifile;
  80. {
  81.    int n;
  82.    char inline[MAXLINE], outline[MAXLINE], *fgets ();
  83.  
  84.    while ((fgets (inline, MAXLINE, ifile)) != NULL) {
  85.       n = strlen (inline);
  86.       while (--n >= 0)            /* back over white space */
  87.      if (inline[n] != ' ' && inline[n] != '\t' &&
  88.         inline[n] != '\n') break;
  89.       inline[n+1] = '\0';
  90.       expand (inline, outline, MAXLINE);
  91.       puts (outline);
  92.    }
  93. }
  94.  
  95.  
  96. /* expand - expand a line */
  97.  
  98. expand (in, out, lim)
  99. char in[], out[];
  100. int  lim;
  101. {
  102.    register int i, j;
  103.    register char ch;
  104.  
  105.    i = j = 0;
  106.    while (((ch = in[i++]) != '\0') && (j < lim)) {
  107.       if (ch == '\t') {
  108.      register int k;
  109.      register char tc;
  110.  
  111.      k = j;
  112.      out[j++] = ' ';
  113.      while ((j < lim) && ((tc = tabarray[j]) == '\0'))
  114.         out[j++] = ' ';
  115.      if (tc == '\0') {
  116.         j = k;
  117.         out[j++] = ch;
  118.      }
  119.       } else
  120.      out[j++] = ch;
  121.    }
  122.    out[j] = ch;
  123. }
  124.  
  125.  
  126. /* wb_parse is included here only to reduce the size of the executable */
  127.  
  128. void
  129. _wb_parse ()
  130. {
  131. }
  132.